﻿#region Header

/*
    This file is part of ndoctor-statistics.

    ndoctor-statistics is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ndoctor-statistics is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ndoctor-statistics.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.NDoctor.Statistics.FactoriesImpl
{
    using System.Collections.Generic;

    using Castle.DynamicProxy;

    using Probel.NDoctor.Statistics.Components;
    using Probel.NDoctor.Statistics.Domain;
    using Probel.NDoctor.Statistics.RepositoriesImpl;

    internal class MySqlFactory : IFactory
    {
        #region Fields

        private static readonly ProxyGenerator Generator = new ProxyGenerator(new PersistentProxyBuilder());

        private readonly ISessionRepository Repository;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MySqlFactory"/> class.
        /// </summary>
        /// <param name="path">The path.</param>
        public MySqlFactory(string server, string username, string password, string database, bool doUpdateDatabase = false, bool doCreateNewDatabase = false)
        {
            this.Repository = new MySqlRepository(server, username, password, database, doUpdateDatabase, doCreateNewDatabase);
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Queries that return charts
        /// </summary>
        /// <returns></returns>
        public IChartQuery Charts()
        {
            var instance = new ChartQuery(this.Repository);
            return Generator.CreateInterfaceProxyWithTarget<IChartQuery>(instance, new SessionManagerInterceptor());
        }

        /// <summary>
        /// Get queries that returns collections.
        /// </summary>
        /// <returns></returns>
        public ICollectionQuery Collections()
        {
            var instance = new CollectionQuery(this.Repository);
            return Generator.CreateInterfaceProxyWithTarget<ICollectionQuery>(instance, new SessionManagerInterceptor());
        }

        /// <summary>
        /// Get the data exporter
        /// </summary>
        /// <returns></returns>
        public IStatisticsExporter Exporter()
        {
            var instance = new StatisticsExporter(this.Repository);
            return Generator.CreateInterfaceProxyWithTarget<IStatisticsExporter>(instance, new SessionManagerInterceptor());
        }

        public void Import(IEnumerable<Application> applications, IEnumerable<StatisticEntry> statistics, IEnumerable<SessionDuration> durations)
        {
            this.Repository.Import(applications, statistics, durations);
        }

        /// <summary>
        /// Get queries that return scalar result
        /// </summary>
        /// <returns></returns>
        public IScalarQuery Scalars()
        {
            var instance = new ScalarQuery(this.Repository);
            return Generator.CreateInterfaceProxyWithTarget<IScalarQuery>(instance, new SessionManagerInterceptor());
        }

        #endregion Methods
    }
}